﻿using Core;
using RimWorld;
using System.Collections.Generic;
using Verse;
using Verse.AI;
using WhoXiuXian;

namespace RI_Talisman
{
    public class JobDriver_OpenBlindBox : JobDriver_StudyItem
    {
        private const TargetIndex ResearchBenchInd = TargetIndex.B;

        private const TargetIndex HaulCell = TargetIndex.C;

        protected const TargetIndex StudiableInd = TargetIndex.A;

        private const float DefaultResearchSpeed = 0.08f;

        private const int JobEndInterval = 4000;

        protected Thing StudyThing => base.TargetThingA;

        protected CompBlindBoxUsing StudyComp => StudyThing.TryGetComp<CompBlindBoxUsing>();

        public override bool TryMakePreToilReservations(bool errorOnFailed)
        {
            if (pawn.Reserve(base.TargetThingA, job, 1, -1, null, errorOnFailed) && pawn.Reserve(base.TargetB, job, 1, -1, null, errorOnFailed))
            {
                return pawn.Reserve(base.TargetC, job, 1, -1, null, errorOnFailed);
            }

            return false;
        }

        protected override IEnumerable<Toil> MakeNewToils()
        {
            this.FailOnDespawnedNullOrForbidden(TargetIndex.B);
            this.FailOnBurningImmobile(TargetIndex.B);
            yield return Toils_General.DoAtomic(delegate
            {
                job.count = 1;
            });
            yield return Toils_Goto.GotoThing(TargetIndex.A, PathEndMode.ClosestTouch).FailOnDespawnedNullOrForbidden(TargetIndex.A).FailOnSomeonePhysicallyInteracting(TargetIndex.A);
            yield return Toils_Haul.StartCarryThing(TargetIndex.A, putRemainderInQueue: false, subtractNumTakenFromJobCount: true).FailOnDestroyedNullOrForbidden(TargetIndex.A);
            yield return Toils_Goto.GotoThing(TargetIndex.B, PathEndMode.InteractionCell);
            yield return Toils_Haul.PlaceHauledThingInCell(TargetIndex.C, null, storageMode: false);
            yield return BookStudyToil();
        }

        private Toil BookStudyToil()
        {
            Toil study = ToilMaker.MakeToil("StudyToil");
            study.tickAction = delegate
            {
                Pawn actor = study.actor;
                float num = 0.08f;
                if (!actor.WorkTypeIsDisabled(WorkTypeDefOf.Research))
                {
                    num = actor.GetStatValue(StatDefOf.ResearchSpeed);
                }
                num *= base.TargetThingA.GetStatValue(StatDefOf.ResearchSpeedFactor);
                StudyComp.Study(num, actor);
                if (StudyComp.Completed)
                {
                    pawn.jobs.curDriver.ReadyForNextToil();
                }
            };
            study.FailOnCannotTouch(TargetIndex.A, PathEndMode.InteractionCell);
            study.WithProgressBar(TargetIndex.A, () => StudyComp.ProgressPercent);
            study.defaultCompleteMode = ToilCompleteMode.Delay;
            study.defaultDuration = 4000;
            study.activeSkill = () => SkillDefOf.Intellectual;
            return study;
        }
    }
}
